package com.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.ProductType;
import com.service.ProductTypeService;
import com.service.impl.ProductTypeServiceImpl;

@WebServlet("/ProductTypeController")
public class ProductTypeController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ProductTypeController() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*
		 * request.setCharacterEncoding("UTF-8");
		 * response.setCharacterEncoding("UTF-8");
		 * response.setContentType("text/html;charset=UTF-8");
		 */

		ProductTypeService ptd = new ProductTypeServiceImpl();

		response.getWriter().print(ptd.selectProductType());
		response.getWriter().flush();
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*
		 * request.setCharacterEncoding("UTF-8");
		 * response.setCharacterEncoding("UTF-8");
		 * response.setContentType("text/html;charset=UTF-8");
		 */

		String method = request.getParameter("method");

		if ("addProductType".equals(method))
			addProductType(request, response);
		else if ("deleteProductType".equals(method))
			deleteProductType(request, response);
		else if ("updateProductType".equals(method))
			updateProductType(request, response);
		else if ("selectProductType".equals(method))
			selectProductType(request, response);
	}

	// 添加图书类别
	protected void addProductType(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// 获取页面信息
		String id = request.getParameter("id");
		String productTypeName = request.getParameter("productTypeName");
		String description = request.getParameter("description");

		// 添加数据
		ProductType productType = new ProductType();
		productType.setId(Integer.parseInt(id));
		productType.setProductTypeName(productTypeName);
		productType.setDescription(description);

		ProductTypeService ptd = new ProductTypeServiceImpl();
		boolean b = ptd.addProductType(productType);

		String resultJson = null;

		if (b) {
			resultJson = "{\"success\":true,\"msg\":\"添加成功！\"}";
		} else {
			resultJson = "{\"success\":false,\"msg\":\"添加失败!\"}";
		}
		response.getWriter().print(resultJson);
		response.getWriter().flush();
	}

	// 删除图书类别
	protected void deleteProductType(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String id = request.getParameter("id");
		String resultJson = null;

		ProductTypeService ptd = new ProductTypeServiceImpl();
		boolean b = ptd.deleteProductType(Integer.parseInt(id));
		if (b) {
			resultJson = "{\"success\":true,\"msg\":\"删除成功！\"}";
		} else {
			resultJson = "{\"success\":false,\"msg\":\"删除失败!\"}";
		}
		response.getWriter().print(resultJson);
		response.getWriter().flush();
	}

	// 修改图书类别
	protected void updateProductType(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// 获取页面信息
		String id = request.getParameter("id");
		String productTypeName = request.getParameter("productTypeName");
		String description = request.getParameter("description");
		// 添加数据
		ProductType productType = new ProductType();
		productType.setId(Integer.parseInt(id));
		productType.setProductTypeName(productTypeName);
		productType.setDescription(description);

		ProductTypeService ptd = new ProductTypeServiceImpl();
		boolean b = ptd.updateProductType(productType, Integer.parseInt(id));

		String resultJson = null;

		if (b) {
			resultJson = "{\"success\":true,\"msg\":\"修改成功！\"}";
		} else {
			resultJson = "{\"success\":false,\"msg\":\"修改失败!\"}";
		}
		response.getWriter().print(resultJson);
		response.getWriter().flush();
	}

	// 查询图书类别
	protected void selectProductType(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		String key = request.getParameter("key");
		String type = request.getParameter("type");
		String sort = request.getParameter("sort");
		String order = request.getParameter("order");
		int startPage = Integer.parseInt(page);
		int endRows = Integer.parseInt(rows);
		ProductTypeService ptd = new ProductTypeServiceImpl();
		response.getWriter().print(
				ptd.selectProductType(key, type, startPage, endRows, sort,
						order));
		response.getWriter().flush();
	}
}
